package com.dalongxin.work.page;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;
import android.view.WindowManager;

import com.dalongxin.work.R;
import com.dalongxin.work.page.fragment.ContactFragment;
import com.dalongxin.work.page.fragment.HomeFragment;
import com.dalongxin.work.page.fragment.MineFragment;
import com.dalongxin.work.utils.StatusBarUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
/**
 * 主界面
 * @author Yao
 */
@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    @ViewInject(R.id.navigation)
    private BottomNavigationView bottomNavigationView;

    private Fragment[] fragments;
    private HomeFragment homeFragment;
    private ContactFragment contactFragment;
    private MineFragment mineFragment;
    private int lastFragment;//用于记录上个选择的Fragment

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initFragment();
    }

    //初始化fragment和fragment数组
    private void initFragment() {
        homeFragment = HomeFragment.newInstance();
        contactFragment = ContactFragment.newInstance();
        mineFragment = MineFragment.newInstance();
        fragments = new Fragment[]{homeFragment, contactFragment, mineFragment};
        lastFragment = 0;
        getSupportFragmentManager().beginTransaction().replace(R.id.content, homeFragment).show(homeFragment).commit();
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
        StatusBarUtil.darkMode(this, true);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_index_home:
                StatusBarUtil.darkMode(this, true);
                if(lastFragment != 0) {
                    switchFragment(lastFragment,0);
                    lastFragment = 0;
                }
                return true;
            case R.id.navigation_index_contact:
                StatusBarUtil.darkMode(this, false);
                if(lastFragment != 1) {
                    switchFragment(lastFragment,1);
                    lastFragment = 1;
                }
                return true;
            case R.id.navigation_index_mine:
                StatusBarUtil.darkMode(this, false);
                if(lastFragment != 2) {
                    switchFragment(lastFragment,2);
                    lastFragment = 2;
                }
                return true;
            }
        return false;
    }

    //切换Fragment
    private void switchFragment(int lastfragment,int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments[lastfragment]);//隐藏上个Fragment
        if(fragments[index].isAdded()==false) {
            transaction.add(R.id.content,fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
    }

}
